﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using LRSTaxServices.Biz;
using LRSTaxServices.Common;

public partial class AdminSection_ManageContactUs : System.Web.UI.Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_ContactUs = "__ContactUs::ContactUs";
    private const string PROP_LAYOUT = "__ContactUs::UIL";
    private const string PROP_SELECTED_ContactUs_ID = "__ContactUs::SContactUsID";

    #endregion

    #region Public Properties

    public ContactUsCollection DataSourceContactUs
    {
        get { return (SessionMgr.Session[PROP_DATASRC_ContactUs] is ContactUsCollection ? (ContactUsCollection)SessionMgr.Session[PROP_DATASRC_ContactUs] : (ContactUsCollection)(SessionMgr.Session[PROP_DATASRC_ContactUs] = new ContactUsCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_ContactUs] = value; }
    }

    public int SelectedContactUsID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_ContactUs_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_ContactUs_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_ContactUs_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceContactUs = ContactUs.GetAllContactUs();
            ContactUsList1.DataBind(DataSourceContactUs);
        }
    }

    #endregion

    #region Control: ContactUsList

    protected void ucContactUsList_EditRequested(object sender, Controls_ContactUsList.EditRequestedEventArgs evt)
    {
        SelectedContactUsID = evt.CU.ContactUsID;
        ContactUsViewEdit1.DataBind(evt.CU);
        SetLayout(LayoutType.Edit);
    }

    protected void ucContactUsList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceContactUs = ContactUs.GetAllContactUs();
        ContactUsList1.DataSource = DataSourceContactUs;
    }

    #endregion

    #region Control: ContactUsViewEdit

    protected void ucContactUsViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        ContactUsViewEdit1.DataSourceContactUs = DataSourceContactUs.FindByContactUsID(SelectedContactUsID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btneditCancel_Click(object sender, ImageClickEventArgs e)
    {
        ContactUsViewEdit1.CancelEdit();
        // If we were adding a new jobcategory...then remove the incomplete/not added new jobcategory object from the collection.
        if (DataSourceContactUs.FindByContactUsID(SelectedContactUsID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceContactUs.Remove(DataSourceContactUs.FindByContactUsID(SelectedContactUsID));
                SelectedContactUsID = (DataSourceContactUs[0] != null ? DataSourceContactUs[0].ContactUsID : 0);
            }
            catch
            {
            }
        }
        ContactUsList1.DataBind(DataSourceContactUs);
        SetLayout(LayoutType.View);
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlContactUsList.Visible = false;
                pnlContactUsViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlContactUsList.Visible = true;
                pnlContactUsViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion
}
